package com.google.code.chaotic.client;

import static com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_LEFT;
import static com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_RIGHT;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

import com.allen_sauer.gwt.log.client.Log;
import com.google.code.chaotic.dto.LatLonDto;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.BorderStyle;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.http.client.URL;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.LatLngBounds;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;

public class Util {

	public static final String COUNTRY_SUFFIX = ".my";

	private static final LatLng VIEWPORT_SOUTH_EAST = LatLng.newInstance( //
			3.004, 101.808);
	private static final LatLng VIEWPORT_NORTH_WEST = LatLng.newInstance( //
			3.264, 101.506);

	//

	public static void clearElement(Element parent) {
		Element firstChild;
		while ((firstChild = DOM.getFirstChild(parent)) != null) {
			DOM.removeChild(parent, firstChild);
		}
	}

	public static HorizontalAlignmentConstant getReverseAlignment(
			HorizontalAlignmentConstant alignment) {
		return alignment == ALIGN_RIGHT ? ALIGN_LEFT : ALIGN_RIGHT;
	}

	public static void applyDottedBorder(Widget widget, String color) {
		Style style = widget.getElement().getStyle();
		style.setBorderStyle(BorderStyle.DOTTED);
		style.setBorderWidth(1, Unit.PX);
		style.setBorderColor(color);
	}

	public static void maximize(UIObject uiObject) {
		uiObject.setSize("100%", "100%");
	}

	public static LatLngBounds getCityViewport() {
		LatLngBounds viewport = LatLngBounds.newInstance( //
				VIEWPORT_NORTH_WEST, VIEWPORT_SOUTH_EAST);
		return viewport;
	}

	public static Geocoder createGeocoder() {
		Geocoder geocoder = new Geocoder();
		geocoder.setBaseCountryCode(COUNTRY_SUFFIX);
		geocoder.setViewport(getCityViewport());
		return geocoder;
	}

	public static HashMap<String, String> extractParameters(String queryStr) {

		HashMap<String, String> map = new HashMap<String, String>();
		String[] tokens = queryStr.split("\\&");
		Arrays.sort(tokens, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}
		});
		for (String token : tokens) {
			String[] parts = token.split("\\=");
			String part0 = URL.decodeComponent(parts[0]);
			String part1 = parts.length > 1 ? URL.decodeComponent(parts[1])
					: null;
			map.put(part0, part1);

			char[] padding = new char[8 - part0.length()];
			Arrays.fill(padding, ' ');
			part0 = new StringBuilder().append(padding).append(part0)
					.toString();

			Log.debug("Paramter: " + part0 + "\t =   " + part1);
		}
		return map;
	}

	public static LatLonDto toLatLon(LatLng origin) {
		return new LatLonDto(origin.getLatitude(), origin.getLongitude());
	}

}
